import { getDataCriterionDetail, addDataCriterion, updateDataCriterion } from '@/api/test/index';
import { dataType, isDataNull } from '@/enums/statusEnum';
import { dataCriterionStandard } from '@/pages/CodeTables/specification';

const datas = ref<dataCriterionStandard>({
  dcCode: '',
  dcChName: '',
  dcEnName: '',
  dcExplain: '',
  dcSourceStructure: '',
  dcDataLength: '',
  dcValue: '',
  dcDataNull: undefined,
  dcDataType: '',
  dcMaxValue: '',
  dcMinValue: '',
  dcDataPrecision: '',
  stopwatchName: '',
  status: '',
});
//数据标准目录的详情
const Details = async () => {
  const res = await getDataCriterionDetail(datas.value.dcCode);
  res.dcDataNull = isDataNull[+res.dcDataNull];
  res.dcDataType = dataType[+res.dcDataType];
  datas.value = res as dataCriterionStandard;
};
//新增码表
const addNewDataCriterion = async () => {
  await addDataCriterion(datas.value);
  console.log('新增成功');
};
//编辑码表
const updateData = async () => {
  switch (datas.value.dcDataType) {
    case 40040:
      datas.value.dcMaxValue = '';
      datas.value.dcMinValue = '';
      datas.value.dcDataPrecision = '';
      datas.value.stopwatchName = '';
      break;
    case 40041:
      datas.value.dcDataLength = '';
      datas.value.stopwatchName = '';
      datas.value.dcDataPrecision = '';
      break;
    case 40042:
      datas.value.dcDataLength = '';
      datas.value.stopwatchName = '';
      break;
    case 40045:
      datas.value.dcDataLength = '';
      datas.value.dcDataPrecision = '';
      datas.value.dcMaxValue = '';
      datas.value.dcMinValue = '';
      break;
    default:
      break;
  }
  await updateDataCriterion(datas.value);
  console.log('编辑成功');
};

export function getDataCriterionDetailHooks() {
  return {
    Details,
    datas,
    addNewDataCriterion,
    updateData,
  };
}
